<?php
/**
 * 
 * 品牌信息
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-4-25
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/
class Nav extends CActiveRecord {
	public $page_list = array();//分页数组
	public $infos_count = 0;//统计查询个数
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{nav}}';
	}



	/**
	 *更新导航信息
	 */
	public function updateNav($nav_id,$nav_position,$sort){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{nav}} SET nav_position = $nav_position,nav_sort = $sort WHERE nav_id = $nav_id";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	/**
	 * 新增导航
	 */
	public function addNav($addNav){
		if(!isset($addNav['nav_url']) || empty($addNav['nav_url'])){
			$addNav['nav_url'] = '#';
		}
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO {{nav}}(`nav_name`,`parent_id`,`nav_position`,`nav_url`,`nav_sort`,`active`) VALUES ('".$addNav['nav_name']."','".$addNav['parent_id']."','".$addNav['nav_position']."','".$addNav['nav_url']."','".$addNav['nav_sort']."','".$addNav['active']."')";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	/**
	 * 检测是否已经存在
	 */
	public function checkExist($addInfo){
		$cmodel = new CCCModel();
		$result = $cmodel -> sel("SELECT nav_id FROM {{nav}} where nav_name ='".$addInfo['nav_name']."'");
		return $result;
	}
	
	/**
	 * 品牌修改
	 */
	public function NavUpdate($recPost,$nav_id){
		$cmodel = new CCCModel();
		if($recPost['send'] == "修改"){
			$_sql = "UPDATE {{nav}} set 
							nav_name='".$recPost['nav_name']."',
							parent_id='".$recPost['parent_id']."',
							nav_url='".$recPost['nav_url']."',
							nav_sort=".$recPost['nav_sort'].",
							active=".$recPost['active']."
							WHERE nav_id=".$nav_id;
				$row = $cmodel->upd($_sql);
		}
		return $row;
		
	}

	/**
	 * 查找单条记录
	 */
	public function getNavById($nav_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{nav}} WHERE nav_id=".$nav_id;
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	/**
	 * 获得二级搜索导航
	 */
	public function getChildNav($nav_id){
			$cmodel = new CCCModel();
			$_sql = "SELECT nav_name FROM {{nav}}  WHERE parent_id=".$nav_id;
			$result = $cmodel->sel($_sql);
			return $result;
		}
	
	
	/**
	 * 删除品牌
	 */
	public function NavDel($nav_id){
		$cmodel = new CCCModel();
		$_sql = "DELETE from {{nav}} WHERE nav_id=".$nav_id;
		$row = $cmodel->upd($_sql);
		return $row;
	}
	/**
	 * 分页+搜索
	 */
	public function pageSearch($getInfo){
		$_sql = '';
		$cmodel = new CCCModel();
		$_sql .= "SELECT * FROM {{nav}} WHERE 1=1";
		$nextpage = "";
		if ($getInfo){     
			if($_POST['active'] != 3){
				$nextpage .= "/active=".$getInfo['active'];
				$_sql.=" AND active=".$getInfo['active'];
			}
			if($_POST['keyword'] != null){
				$nextpage .= "&keyword=".$getInfo['keyword'];
				$_sql.=" AND nav_name like '%".$getInfo['keyword']."%'";
			}
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
		
		
			/**
			 * 分页
			 */
			$per = 8;
			$this->infos_count = $cmodel->sel($_sql_count);
			$cnt = $this->infos_count[0]['count(*)'];
			$page = new Pagination($cnt,$per,$nextpage);
			$_sql .= " ORDER BY nav_sort ASC $page->limit";
			$infos = $cmodel->sel($_sql);
			$page_list[0] = $page->fpage(array(3));
			$page_list[1] = $page->fpage(array(4));
			$page_list[2] = $page->fpage(array(5));
			$page_list[3] = $page->fpage(array(6));
			$page_list[4] = $page->fpage(array(7));
			
			$this->page_list[0] = $page_list[0];
			$this->page_list[1] = $page_list[1];
			$this->page_list[2] = $page_list[2];
			$this->page_list[3] = $page_list[3];
			$this->page_list[4] = $page_list[4];
			return $infos;
	}
	
	/**
	 * 按照sort查询所有数据
	 */
	public function all(){
		$all_model = new CCCModel();
		$_sql = "SELECT * FROM {{nav}}  ORDER BY nav_sort ASC ";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	/**
	 * 查询导航父级
	 */
	public function parentAll(){
		$all_model = new CCCModel();
		$_sql = "SELECT nav_id,parent_id,nav_name FROM {{nav}} WHERE parent_id=0 ORDER BY nav_sort ASC";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
	public function getParentName(){
		$all_model = new CCCModel();
		$_sql = "SELECT nav_id,nav_name FROM {{nav}}";
		$result = $all_model->sel($_sql);
		return $result;
	}
}
	
?>